package com.ruoyi.app.controller.common.controller;

import com.ruoyi.app.common.constant.CaptchaConstants;
import com.ruoyi.app.common.utils.RandomValidateCodeUtil;
import com.ruoyi.app.controller.common.entity.CaptchaBO;
import com.ruoyi.common.annotation.PassToken;
import com.ruoyi.common.core.page.ResultData;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/api/captcha")
public class CaptchaController {

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @PassToken
    @GetMapping("/get/{flag}/{key}")
    public ModelAndView get(@PathVariable("key") String key, @PathVariable("flag") String flag, HttpServletResponse response) {
        CaptchaConstants.TemplateEnum template = CaptchaConstants.TemplateEnum.match(flag);
        if (StringUtils.isBlank(template.getRedisPrefix())) {
            return null;
        }
        ServletOutputStream out = null;
        try {
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
            response.setHeader("Pragma", "no-cache");
            response.setContentType("image/jpeg");
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            String imageCode = randomValidateCode.getRandcode(response);//输出验证码图片方法
            this.redisTemplate.opsForValue().set(template.getRedisPrefix() + key, imageCode, template.getEffectiveMin(), TimeUnit.MINUTES);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @PassToken
    @PostMapping("/verify")
    @ResponseBody
    public ResultData verify(CaptchaBO captchaBO) {
        if (StringUtils.isBlank(captchaBO.getCode())) {
            return ResultData.error("请输入验证码！");
        }
        CaptchaConstants.TemplateEnum template = CaptchaConstants.TemplateEnum.match(captchaBO.getFlag());
        if (StringUtils.isBlank(template.getRedisPrefix())) {
            return ResultData.error("验证码错误！");
        }
        String code = (String) redisTemplate.opsForValue().get(template.getRedisPrefix() + captchaBO.getKey());
        if (StringUtils.isBlank(code)) {
            return ResultData.error("验证码已过期！");
        }
        if (!code.equalsIgnoreCase(captchaBO.getCode())) {
            return ResultData.error("验证码不正确！");
        }
        this.redisTemplate.delete(template.getRedisPrefix() + captchaBO.getKey());
        return ResultData.success();
    }

}
